package com.woniuxy.micromallserver.ordermag.mapper;

import com.woniuxy.micromallserver.bean.OrderBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author unreleased
 * @date 2021/1/23 10:01
 */
@Mapper
public interface OrderMapper {
    /**
     * 根据查询参数返回订单列表
     *
     * @param orderBean 携带查询参数的orderBean
     * @return 返回查询结果集合
     */
    List<OrderBean> findOrderListByParams(@Param("o") OrderBean orderBean);

    /**
     * 订单发货 更改订单状态
     *
     * @param deliveryId 物流单号
     * @param id         订单id
     * @return 是否更改成功
     */
    @Update("update t_order set delivery_id = #{deliveryId},status = 2,delivery_time = now() where id = #{id}")
    boolean updateDelivery(@Param("deliveryId") String deliveryId, @Param("id") Long id);

    /**
     * 修改订单
     *
     * @param orderBean 订单对象
     * @return 修改成功？
     */
    @Update("<script>" +
            "update t_order" +
            "<set>" +
            "<if test = 'o.status != null'>" +
            "status = #{o.status}" +
            "</if>" +
            "</set>" +
            "where id = #{o.id}" +
            "</script>")
    boolean updateOrder(@Param("o") OrderBean orderBean);

    /**
     * 查询未发货的订单数量
     * @return 数量
     */
    @Select("SELECT count(`status`) FROM t_order where `status` = 1")
    int waitSendingOrderNums();

    /**
     * 插叙本月销售额
     * @return 本月销售额
     */
    @Select("SELECT SUM(actual_price) FROM t_order WHERE DATE_FORMAT( end_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
    BigDecimal findCurrentMonthSales();

    /**
     * 批量关闭订单
     * @param ids 订单id
     * @return int
     */
    int closeAllOrders(@Param("array") int[] ids);
}
